package com.lmf.juc.Lock.case5;

/**
 * 演示死锁的产生过程：
 *
 */
public class DeadLockDemo {

    public static void main(String[] args) {
        //创建两个对象，作为锁
        Object a = new Object();
        Object b = new Object();

        //创建多线程分别获取多个锁
        //创建A线程，持有a锁，试图获取b锁
        new Thread(() -> {
            synchronized (a){
                System.out.println(Thread.currentThread().getName() + "线程：持有a锁，试图获取b锁");
                //等待3秒
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (b){
                    System.out.println(Thread.currentThread().getName() + "线程：持有a锁已经获取到b锁");
                }
            }
        },"A").start();

        //创建A线程，持有b锁，试图获取a锁
        new Thread(() -> {
            synchronized (b){
                System.out.println(Thread.currentThread().getName() + "线程：持有b锁，试图获取a锁");
                //等待3秒
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (a){
                    System.out.println(Thread.currentThread().getName() + "线程：持有b锁已经获取到a锁");
                }
            }
        },"B").start();
    }
}
